package com.jie.common;

import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by IntelliJ IDEA.
 * User: zxm52
 * Date: 2021/6/18
 * Time: 10:13
 */
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Denlong getException(Exception ex) {
        return new Denlong(0, ex.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Denlong formCheckException(MethodArgumentNotValidException ex) {
        String result = "校验失败：";
        for (FieldError error : ex.getFieldErrors()) {
            result += error.getDefaultMessage() + "；";
        }
        return new Denlong(0, result);
    }
}
